#include <stdio.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>

size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;

    // 解析JSON数据
    cJSON *root = cJSON_Parse(result);  // 将JSON数据解析成cJSON对象
    if (root != NULL)
    {
        cJSON *successJson = cJSON_GetObjectItemCaseSensitive(root, "success");
        cJSON *codeJson = cJSON_GetObjectItemCaseSensitive(root, "code");
        cJSON *messageJson = cJSON_GetObjectItemCaseSensitive(root, "message");

        printf("success:%d code:%d message:%s\n",successJson->valueint,codeJson->valueint,messageJson->valuestring);
    }

    printf("%s\n",result);
    return realsize;
}

int main(void)
{
    CURL *curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://47.109.65.208/config.json");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
        CURLcode res = curl_easy_perform(curl);
        if (res!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        curl_easy_cleanup(curl);
    }
    
    return 0;
}